import axios from 'axios'
// const CancelToken = axios.CancelToken
// const source = CancelToken.source()
// 首页获取数据
// 设置基准路径
axios.defaults.baseURL = 'http://129.204.200.3:3000'
// axios.defaults.baseURL = 'http://127.0.0.1:3000'
function getdata (classify, uid, offset) {
  return axios({ url: `/api/data?classify=${classify}&uid=${uid}&offset=${offset}` })
}
// 用户注册
function regUser (data) {
  return axios({
    url: '/users/add',
    method: 'post',
    data
  })
}
// 用户登录
function userLogint (data) {
  // 登录用户的数据
  return axios({
    url: '/users/login',
    method: 'post',
    data
  })
}
// 关注其他用户
function getfollow (uid, nid, isfollow) {
  // uid点关注的用户 nid被关注的用户 isfollw关注的状态
  return axios({
    url: `/api/follow?uid=${uid}&nid=${nid}&isfollow=${isfollow}`,
    method: 'get'
  })
}
// 取消关注用户 传递fid
function delfollow (fid) {
  // fid是 关注记录的id
  return axios({
    url: `/api/delfollow?fid=${fid}`,
    method: 'get'
  })
}
// 点赞文章
function getFabulous (uid, cid, aid) {
  // uid 是点赞用户的id cid 是文章id aid是文章作者的id
  return axios({
    url: `/api/artFabulous?uid=${uid}&cid=${cid}&aid=${aid}`,
    method: 'get'
  })
}
// 取消点赞文章
function remFabulous (uid, cid) {
  // uid是点赞者的id cid 是文章id
  return axios({
    url: `/api/delartFabulous?uid=${uid}&cid=${cid}`,
    method: 'get'
  })
}
// 评论文章
function subComment (uid, cid, comment) {
  // uid是评论者的id cid是文章id  comment 评论内容
  return axios({
    url: `/api/subcomment?uid=${uid}&cid=${cid}&comment=${comment}`,
    method: 'get'
  })
}
// 首页获取评论数据 文章一级
function getComments (cid, uid, page, sort, more = 0) {
  // cid 文章id offset 偏移量  sort 排序方式 more 判断当前是否进入文章详情页，进入文章详情页每页请求数据量翻倍
  return axios({ url: `/api/commentdata?cid=${cid}&uid=${uid}&page=${page}&sort=${sort}&more=${more}` })
}
// 获取消息页被点赞的数据
function getPraise (uid, page) {
  // uid是用户登录的id
  return axios({ url: `/news/newsdata?uid=${uid}&page=${page}` })
}
// 获取消息页评论的未读信息条数
function getFabUnread (uid) {
  // uid是用户登录的id
  return axios({ url: `/news/getFabUnread?uid=${uid}` })
}
// 将当前用户读取到的未读--评论--消息修改成已读
function updateComList (uid) {
  // uid是用户登录的id
  return axios({ url: `/news/updateComList?uid=${uid}` })
}
// 将当前用户读取到的未读--点赞--消息修改成已读
function updateFabList (uid) {
  // uid是用户登录的id
  return axios({ url: `/news/updateFabList?uid=${uid}` })
}
// 获取消息页点赞的未读信息条数
function getcommentUnread (uid) {
  // uid是用户登录的id
  return axios({ url: `/news/getcommentUnread?uid=${uid}` })
}
// 获取消息页的全部评论的数据
function newcomment (uid, page) {
  // uid是用户登录的id
  return axios({ url: `/news/newscomment?uid=${uid}&page=${page}` })
}
// 获取消息页的我关注的人的数据
function followcomment (uid, page) {
  // uid是用户登录的id
  return axios({ url: `/news/followcomment?uid=${uid}&page=${page}` })
}
// 获取消息页的我发出的评论的数据
function mycomment (uid, page) {
  // uid是用户登录的id
  return axios({ url: `/news/mycomment?uid=${uid}&page=${page}` })
}

// 收藏文章
function getCollect (uid, cid) {
  // 收藏者的uid 和文章id
  return axios({ url: `/api/articlecollect?uid=${uid}&cid=${cid}` })
}

// 取消收藏文章
function delCollect (colId) {
  // colId是收藏记录的id
  return axios({ url: `/api/delcollect?col_id=${colId}` })
}
// 文章更多选项状态
function articleState (uid, nid, cid) {
  // uid是当前登录用户的id
  // nid是文章作者的id
  // cid是文章id
  return axios({ url: `/api/articlemore?uid=${uid}&nid=${nid}&cid=${cid}` })
}

// 文章详情页面
function articledetail (uid, cid, userId = 0) {
  // uid是文章作者的id cid 文章id  userId登录用户的id
  return axios({ url: `/api/articledetail?uid=${uid}&cid=${cid}&userId=${userId}` })
}
// 可能感兴趣的用户的推荐数据
function userinterested (uid) {
  // uid 登录用户的id
  return axios({ url: `/api/userinterested?uid=${uid}` })
}
// 用户详情页
function userinfo (uid, uuid) {
  // uid 登录用户的id uuid是被查看用户的id
  return axios({ url: `/users/userInfo?uid=${uid}&uuid=${uuid}` })
}
// 用户详情页 精选内容
function userSelected (uid, uuid, page) {
  // uid 显示内容的用户的id
  // uuid 查看用户的id
  // page 查看用户的页码
  return axios({ url: `/users/userSelected?uid=${uid}&uuid=${uuid}&page=${page}` })
}
// 用户详情页 全部文章
function userMyArticle (uid, uuid, page) {
  // uid 显示内容的用户的id
  // uuid 查看用户的id
  // page 查看用户的页码
  return axios({ url: `/users/userMyArticle?uid=${uid}&uuid=${uuid}&page=${page}` })
}
// 粉丝列表页
function userfans (uid, page, sort = 0) {
  // uid 登录用户的id page 页码 sort 排序方式
  return axios({ url: `/users/myfans?uid=${uid}&page=${page}&sort=${sort}` })
}
// 粉丝列表页
function userfollow (uid, page, sort = 0, keyword = '') {
  // uid 登录用户的id uuid是被查看用户的id
  return axios({ url: `/users/myfollow?uid=${uid}&page=${page}&sort=${sort}&keyword=${keyword}` })
}
// 用户页取消关注 或者移除粉丝
function userdelfollow (uid, nid) {
  // uid是关注的用户 nid是被关注的用户
  console.log('取消关注的id', uid, nid)
  return axios({ url: `/users/delfollow?uid=${uid}&nid=${nid}` })
}
// 个人页 查询用户和列表单个用户之间的关注关系
function userfollowState (uid, nid) {
  // uid是当前登录用户的id
  // nid是列表用户的id
  return axios({ url: `/users/followState?uid=${uid}&nid=${nid}` })
}
// 我的收藏
function usermycollect (uid, page) {
  // uid是关注的用户 nid是被关注的用户
  return axios({ url: `/users/mycollect?uid=${uid}&page=${page}` })
}
// 视频页面的轮播图
function getbannerList () {
  return axios('/video/bannerList')
}
// 推荐的视频
function getrecommend (page) {
  //
  return axios({ url: `/video/getrecommend?page=${page}` })
}
// 热门视频推荐
function gethotTv (page, offset) {
  //
  return axios({ url: `/video/gethotTv?page=${page}&offset=${offset}` })
}
// 我关注的人发布的视频
function getfollowTv (uid, page) {
  //
  return axios({ url: `/video/followTv?uid=${uid}&page=${page}` })
}
// 推荐的视频号数据
function hotTvUser (page) {
  //
  return axios({ url: `/video/hotTvUser?page=${page}` })
}
// 推荐的视频号与登录用户的关注关系
function getFollowState (uid, uuid) {
  //
  return axios({ url: `/video/getFollowState?uid=${uid}8&uuid=${uuid}` })
}
// 获取 排行榜数据
function getHotList (data) {
  //
  return axios({
    url: '/video/getHotList',
    method: 'post',
    data
  })
}
/// /查询优质视频号推荐包含视频的数据
function getUserlog () {
  //
  return axios('/video/getUserlog')
}
// 获取 视频详情的视频和用户数据
function geTvDetail (data) {
  /// / 传递当前查看用户的id 和视频的id
  return axios({
    url: '/video/geTvDetail',
    method: 'post',
    data
  })
}
// 视频详情页面 推荐的相关视频
function getTvRelevant (vid) {
  //
  return axios({ url: `/video/getTvRelevant?vid=${vid}` })
}
// 增加播放量和记录观看历史
function addTvhistory (uid, vid) {
  // 传递观看者的id和视频的id
  return axios({ url: `/video/addTvhistory?uid=${uid}&vid=${vid}` })
}
// 查看我的观看历史
function getMyHistory (uid, page) {
  // 传递观看者的id和视频的id
  return axios({ url: `/video/getMyHistory?uid=${uid}&page=${page}` })
}
// 点赞或取消点赞视频 详情页的视频vid
function getfab (uid, vid) {
  // 传递观看者的id和视频的id
  return axios({ url: `/video/getfab?uid=${uid}&vid=${vid}` })
}
// 收藏或取消收藏视频 详情页的视频vid
function getcollect (uid, vid) {
  // 传递观看者的id和视频的id
  return axios({ url: `/video/getcollect?uid=${uid}&vid=${vid}` })
}
// 搜索页数据 获取搜索的文章结国
function getarticle (serachText, page) {
  return axios({ url: `/serach/getarticle?serachText=${serachText}&page=${page}` })
}
// 搜索页数据 获取搜索的用户结果
function getuserlist (uid, serachText, page, classify) {
  return axios({ url: `/serach/getuserlist?serachText=${serachText}&page=${page}&uid=${uid}&classify=${classify}` })
}
// 搜索页数据 获取视频的视频结果
function getVideolist (serachText, page, sort) {
  return axios({ url: `/serach/getVideolist?serachText=${serachText}&page=${page}&sort=${sort}` })
}
// 获取本周精选文章数据
function gethotweek () {
  return axios('/api/hotweek')
}
// 获取视频详情的评论数据
function getCommentlist (vid, page, sort = 'new') {
  return axios({ url: `/video/getCommentlist?vid=${vid}&page=${page}&sort=${sort}` })
}
// 点赞评论内容
function getfabcomment (uid, pid, cid) {
  return axios({ url: `/api/getfabcomment?uid=${uid}&pid=${pid}&cid=${cid}` })
}
// 搜索页数据 获取视频的视频结果
function addTvComment (data) {
  return axios({
    url: '/video/addTvComment',
    method: 'post',
    data
  })
}
// 上传文章图片
function addarticlePic (data) {
  return axios({
    url: '/upload/addarticlePic',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}
// 删除上传的文章图片
function deletearticlePic (filename) {
  return axios({ url: `/upload/deletearticlePic?filename=${filename}` })
}
// 发布无图片无视频文章
function addarticleNo (data) {
  return axios({
    url: '/upload/addarticle_no',
    method: 'post',
    data

  })
}
// 发布图片文章
function addarticle (data) {
  return axios({
    url: '/upload/addarticle',
    method: 'post',
    data

  })
}
// 发布视频文章
function uploadArticleCover (data) {
  return axios({
    url: '/upload/upload_article_cover',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}

// 获取已经上传文章视频文件的切片
function uploadAlready (HASH) {
  return axios({ url: `/upload/upload_already?HASH=${HASH}` })
}
// 上传文章视频的文件切片
function uploadChunk (data) {
  return axios({
    url: '/upload/upload_chunk',
    method: 'post',
    data,

    headers: { 'Content-Type': 'multipart/form-data' }

  })
}
// 取消视频文件的上传
// function cancelUpload () {
//   source.cancel('Operation canceled by the user.')
// }
// 上传文章视频的文件切片
function uploadMerge (data) {
  return axios({
    url: '/upload/upload_merge',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}
/// /////////////////////////视频页的视频//////////////////
// 获取已经上传视频文件的切片
function uploadVideoAlready (HASH) {
  return axios({ url: `/video/uploadVideoAlready?HASH=${HASH}` })
}
// 上传视频页的视频的所有切片
function uploadVideoChunk (data) {
  return axios({
    url: '/video/uploadVideoChunk',
    method: 'post',
    data,

    headers: { 'Content-Type': 'multipart/form-data' }

  })
}

// 取消视频文件的上传
// function cancelUpload () {
//   source.cancel('Operation canceled by the user.')
// }
// 合并上传的视频页视频的所有切片
function uploadVideoMerge (data) {
  return axios({
    url: '/video/uploadVideoMerge',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}
// 发布视频页的视频
function addVideoPage (data) {
  return axios({
    url: '/video/pagevide',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}
// 上传用户背景图片
function upUserBg (data) {
  return axios({
    url: '/users/up_user_bg',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }

  })
}
// 点赞评论内容
function getmydata (uid) {
  return axios({ url: `/users/mydata?uid=${uid}` })
}
// 修改文章的展示状态
function EditPostStatus (cid, state) {
  return axios({ url: `/users/EditPostStatus?cid=${cid}&state=${state}` })
}

// 获取首页广告信息
function getHomePoster () {
  return axios('/api/getHomePoster')
}
// 视频回复评论内容
function appendReply (data) {
  return axios({
    url: '/video/appendReply',
    method: 'post',
    data

  })
}
// 获取视频页的二级评论列表
function getReplyList (replyPage, repPid, uid, vid) {
  return axios({ url: `/video/getReplyList?replyPage=${replyPage}&rep_pid=${repPid}&uid=${uid}&vid=${vid}` })
}
// 删除文章
function delarticle (cid) {
  return axios({ url: `/system/delarticle?cid=${cid}` })
}
// 文章回复评论内容
function appendArcReply (data) {
  return axios({
    url: '/api/appendArcReply',
    method: 'post',
    data

  })
}
// 上传用户头像
function updateAvatar (data) {
  return axios({
    url: '/users/updateAvatar',
    method: 'post',
    data,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}
// 获取文章回复评论列表数据
function getArcList (replyPage, repPid, state, cid, uid) {
  return axios({ url: `/api/getArcList?rep_pid=${repPid}&state=${state}&replyPage=${replyPage}&cid=${cid}&uid=${uid}` })
}
// 更改用户数据
function updateUserData (data) {
  return axios({
    url: '/users/updateUserData',
    method: 'post',
    data
  })
}
// 记录广告的被点击次数
function appendAdvNum (advId, uid) {
  return axios({ url: `/api/appendAdvNum?adv_id=${advId}&uid=${uid}` })
}
// 获取详情页的广告
function getDetailAdv () {
  return axios('/video/getDetailAdv')
}
// 删除视频观看的全部历史记录
function delhistory (uid) {
  return axios({ url: `/video/delhistory?uid=${uid}` })
}
// 点赞视频一级评论评论
function getVideoCom (vid, pid, uid) {
  return axios({ url: `/video/getVideoCom?video_id=${vid}&com_fab_uid=${uid}&video_pid=${pid}` })
}
// 点赞视频二级评论评论
function getVideoReply (vid, uid, pid, onePid) {
  return axios({ url: `/video/getVideoReply?vid=${vid}&uid=${uid}&pid=${pid}&one_pid=${onePid}` })
}
// 点赞文章二级评论
function appendArtReply (cid, uid, pid, onePid) {
  return axios({ url: `/api/appendArtReply?cid=${cid}&uid=${uid}&pid=${pid}&one_pid=${onePid}` })
}
// 点击文章视频观看记录
function appvideoHistory (cid) {
  return axios({ url: `/api/appvideoHistory?cid=${cid}` })
}
// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  // console.log('请求发出前执行的方法')
  // const token = localStorageItem('token')
  // config.headers.Authorization = `Bearer ${token}`
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Any status code that lie within the range of 2xx cause this function to trigger
  // Do something with response data
  // console.log('请求成功后第一调用的函数')
  const { authorization } = response.headers // 获取token
  // console.log(response)
  authorization && localStorage.setItem('token', authorization)
  return response
}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  return Promise.reject(error)
})

export default {
  getdata,
  regUser,
  userLogint,
  getfollow,
  getFabulous,
  remFabulous,
  subComment,
  getComments,
  getPraise,
  newcomment,
  getCollect,
  articleState,
  delCollect,
  delfollow,
  articledetail,
  userinterested,
  userinfo,
  userfans,
  userfollow,
  userdelfollow,
  userfollowState,
  userSelected,
  userMyArticle,
  usermycollect,
  followcomment,
  mycomment,
  getrecommend,
  getbannerList,
  getfollowTv,
  gethotTv,
  hotTvUser,
  getFollowState,
  getUserlog,
  getHotList,
  geTvDetail,
  getTvRelevant,
  addTvhistory,
  getMyHistory,
  getfab,
  getcollect,
  getarticle,
  getuserlist,
  getVideolist,
  gethotweek,
  addTvComment,
  getCommentlist,
  getfabcomment,
  addarticlePic,
  deletearticlePic,
  addarticle,
  uploadAlready,
  uploadChunk,
  uploadMerge,
  uploadArticleCover,
  addarticleNo,
  uploadVideoAlready,
  uploadVideoChunk,
  uploadVideoMerge,
  addVideoPage,
  upUserBg,
  getmydata,
  getHomePoster,
  appendReply,
  getReplyList,
  EditPostStatus,
  delarticle,
  appendArcReply,
  getArcList,
  updateAvatar,
  updateUserData,
  appendAdvNum,
  getDetailAdv,
  delhistory,
  getVideoCom,
  getVideoReply,
  appendArtReply,
  appvideoHistory,
  getFabUnread,
  getcommentUnread,
  updateComList,
  updateFabList

}
